package com.example.environment.tools;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemWriter;

import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.Date;
import java.util.UUID;
@Component
public class JwtUtil {

    private static final long EXPIRATION_TIME = 1000 * 60 * 60 * 24; // 24 hours

    private static final String PRIVATE_KEY_FILE = "src/main/resources/keys/private.pem";
    private static final String PUBLIC_KEY_FILE = "src/main/resources/keys/public.pem";


    public static String generateToken(String subject) {
        try {

//            byte[] publicKeyBytes = loadKeyBytes(PUBLIC_KEY_FILE, "PUBLIC KEY");
//            X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
//            KeyFactory keyFactory = KeyFactory.getInstance("RSA");

            PrivateKey privateKey = RsaUtils.getKeyPair().getPrivate();
            Claims claims = Jwts.claims().setSubject(subject);
            return Jwts.builder()
                    .setId(UUID.randomUUID().toString())
                    .setClaims(claims)
                    .setIssuedAt(new Date())
                    .setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME)) // 1小时后过期
                    .signWith(privateKey, SignatureAlgorithm.RS256)
                    .compact();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getStaffId(String token){
        try {
            Claims claims=getClaims(token);
            assert claims != null;
            if(claims.getSubject()!=null){
                return claims.getSubject();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

//    private static byte[] loadKeyBytes(String filePath, String keyType) throws Exception {
//        String keyContent = new String(Files.readAllBytes(Paths.get(filePath)))
//                .replace("-----BEGIN " + keyType + "-----", "")
//                .replace("-----END " + keyType + "-----", "")
//                .replaceAll("\\s", "");
//        return Base64.getDecoder().decode(keyContent);
//    }
    private static Claims getClaims(String token) {
        try {
            PublicKey publicKey = RsaUtils.getKeyPair().getPublic();
            return Jwts.parserBuilder()
                    .setSigningKey(publicKey)
                    .build()
                    .parseClaimsJws(token).getBody();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
